package com.jp.knowledge.service;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.support.v4.content.LocalBroadcastManager;

import com.google.gson.JsonObject;
import com.jp.knowledge.broad.LocalBroadCast;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.model.InteractDataModel;
import com.jp.knowledge.util.ResultSubscriber;

/**
 * Created by 528787067@qq.com on 2017/7/20.
 */

public class LatestStateService extends BaseService implements ResultSubscriber.OnResultListener {

	private static final long DELAY_TIME = 10000;

	private static volatile InteractDataModel interactDataModel;
	private static volatile String preRecordId;

	public static InteractDataModel getInteractDataModel(){
		return interactDataModel;
	}

	public static void clearBadge(){
		if(interactDataModel != null && interactDataModel.getBadge() != null) {
			preRecordId = interactDataModel.getBadge().getRecordId();
			interactDataModel.getBadge().setRecordId(null);
			updateInteractData();
		}
	}

	public static void clearSystemMsg(){
		if(interactDataModel != null) {
			interactDataModel.setSystemMsgCount(0);
			updateInteractData();
		}
	}

	public static void clearCompanyMsg(String companyId){
		if(interactDataModel == null || interactDataModel.getList() == null || companyId == null)
			return;
		for(InteractDataModel.ListBean listBean : interactDataModel.getList()){
			if(companyId.equals(listBean.getCompanyId())){
				int msgCount = listBean.getCount();
				listBean.setCount(0);
				interactDataModel.setCount(interactDataModel.getCount()-msgCount);
				updateInteractData();
				return;
			}
		}
	}

	public static void clearAll(){
		interactDataModel = null;
		updateInteractData();
	}

	public static void updateInteractData(){
		updateInteractData(null);
	}

	public static void updateInteractData(Context context){
		LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(context);
		if(localBroadcastManager != null)
			localBroadcastManager.sendBroadcast(new Intent(LocalBroadCast.GET_INTERACT_DATA_CODE));
	}

	private Handler handler;
	private Runnable runnable;
	private SharedPreferences sharedPreferences;

	@Override
	public void onCreate() {
		super.onCreate();
		sharedPreferences = getSharedPreferences("badge", MODE_PRIVATE);
		preRecordId = sharedPreferences.getString("recordId", null);
		handler = new Handler();
		runnable = new Runnable() {
			@Override
			public void run() {
				getLatestState();
				handler.postDelayed(runnable, DELAY_TIME);
			}
		};
		handler.post(runnable);
	}

	@Override
	public void onDestroy() {
		handler.removeCallbacksAndMessages(null);
		super.onDestroy();
	}

	private void getLatestState(){
		HttpHelper.getInstance(this).interactData(new JsonObject(), 0x01, this);
	}

	@Override
	public void onStart(int requestType) {}
	@Override
	public void onCompleted(int requestType) {}
	@Override
	public void onError(int requestType) {}
	@Override
	public void onNext(IModel data, int requestType) {
		if(data.getErrcode() != 0)
			return;
		interactDataModel = data.getEntity(InteractDataModel.class);
		if(interactDataModel != null && interactDataModel.getBadge() != null && preRecordId != null){
			if(preRecordId.equals(interactDataModel.getBadge().getRecordId())) {
				interactDataModel.getBadge().setRecordId(null);
				if (!preRecordId.equals(sharedPreferences.getString("recordId", null)))
					sharedPreferences.edit().putString("recordId", preRecordId).apply();
			}
		}
		updateInteractData(this);
	}
}
